<template>
<div class="container">
  <div class="wrapper" ref="scroll">
    <section id="city-list" class="city-list-container" style="display: block;">
      <section>
        <div id="locate" class="city-title">
          定位城市
        </div>
        <div class="city-list city-list-inline clearfix">
          <div class="location-city">定位失败，请点击重试</div>
        </div>
      </section>

      <section class="history-city-list">
        <div id="history" class="city-title">
          最近访问城市
        </div>
        <div class="city-list city-list-inline clearfix">

          <div class="city-item" v-for="c in hotCities" :key="c.id">
            {{c.name}}
          </div>

        </div>
      </section>

      <section>
        <div id="hot" class="city-title">
          热门城市
        </div>
        <div class="city-list city-list-inline clearfix">

          <div class="city-item" v-for="c in hotCities" @click="selectCity(c)" :key="c.id">
            {{c.name}}
          </div>

        </div>
      </section>

      <section>

        <div v-for="(values,key) in cities" :key="key">
          <div :id="key" class="city-title city-title-letter">
            {{key}}
          </div>
          <div class="city-list city-list-block clearfix">
            <div v-for="c in values" @click="selectCity(c)" class="city-item" :key="c.id">
              {{c.name}}
            </div>
          </div>
        </div>

      </section>

      
    </section>

  </div>

  <section class="nav">
        <div class="  nav-item" @click="go('locate')" data-id="locate">
          定位
        </div>

        <div class="  nav-item"  @click="go('history')"  data-id="history">
          最近
        </div>

        <div class="  nav-item"  @click="go('hot')" data-id="hot">
          热门
        </div>

        <div class="nav-letter  nav-item" @touchmove="move" v-for="l in getLetters" :key="l" :ref="l">
          {{l}}
        </div>

      </section>
</div>


</template>

<script>
import { get } from "@/api/http"
import BScroll from "@better-scroll/core"
import Pullup from "@better-scroll/pull-up"
BScroll.use(Pullup)

export default {
  data() {
    return {
      cities: {},
      hotCities: []
    }
  },
  computed: {
    getLetters() {
      return Object.keys(this.cities)
    }
  },
  async created() {
    let rs = await get("/cities.json")
    this.cities = rs.data.cities
    this.hotCities = rs.data.hotCities
    console.log(rs)
    setTimeout(() => {
      this.initScroll()
    }, 0)
  },
  methods: {
    selectCity(city) {
      this.$store.commit("updateCity", city)
      this.$router.go(-1)
    },
    move(event) {
      //字母A距离页面顶部的高度
      let aTop = this.$refs.A[0].getBoundingClientRect().y
      //每个字母占据的高度
      let spanHeight = this.$refs.A[0].getBoundingClientRect().height
      //当前触摸位置的y坐标值
      let currentY = event.touches[0].pageY
      //在字母列表当中的索引值
      let index = Math.floor((currentY - aTop) / spanHeight)
      let letter = this.getLetters[index]
      console.log(letter)
      this.scroll.scrollToElement(document.getElementById(letter),300)
    },
    go(id){
      this.scroll.scrollToElement(document.getElementById(id),300)
    },
    initScroll() {
      this.scroll = new BScroll(this.$refs.scroll, {
        scrollY: true,
        pullUpLoad: true,
        //允许点击事件的传递
        click:true
      })
    }
  }
}
</script>


<style lang="scss" scoped>
.container,.wrapper{
  height: 100%;
  overflow: hidden;
}
.city-list-container {
  background-color: #ebebeb;
  font-size: 14px;
  color: #333;
  width: 100%;
}

.clearfix:after {
  content: " ";
  display: table;
  clear: both;
}

.city-title {
  padding-left: 15px;
  line-height: 30px;
}

.city-title-letter {
  padding-left: 25px;
}

.city-list {
  padding-right: 30px;
}

.city-list-inline {
  background-color: #f5f5f5;
  padding-bottom: 8px;
}

.city-list-inline .city-item,
.city-list-inline .location-city {
  float: left;
  background: #fff;
  width: 29%;
  height: 33px;
  margin-top: 15px;
  margin-left: 4%;
  padding: 0 4px;
  border: 1px solid #e6e6e6;
  border-radius: 3px;
  line-height: 33px;
  text-align: center;
  box-sizing: border-box;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.city-list-inline .location-city {
  width: auto;
  min-width: 30%;
  padding: 0 20px;
}

.city-list-block {
  background-color: #f5f5f5;
}

.city-list-block .city-item {
  height: 44px;
  line-height: 44px;
  margin-left: 15px;
  border-bottom: 1px solid #c8c7cc;
}

.city-list-block .city-item:last-child {
  border-bottom: none;
}

.nav {
  position: fixed;
  top: 75px;
  top: 11vh;
  right: 0;
  width: 35px;
  z-index: 10;
  text-align: center;
  font-size: 12px;
}

.nav .nav-item {
  height: 16px;
  height: 2.8vh;
}

.nav .nav-letter {
  width: 15px;
  margin-left: 15px;
}
</style>

